package com.joysuch.wwyt.bp.service.impl;

import cn.zlg.common.util.CollectionUtils;
import com.joysuch.wwyt.bp.bean.inspect.BPInspectPointTypeBean;
import com.joysuch.wwyt.bp.entity.inspect.BPInspectPointType;
import com.joysuch.wwyt.bp.repository.BPInspectPointRepository;
import com.joysuch.wwyt.bp.repository.BPInspectPointTypeRepository;
import com.joysuch.wwyt.bp.service.BPInspectPointTypeService;
import com.joysuch.wwyt.core.bean.SimpleSelectListBean;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.util.PageableUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 巡检： 巡检点类型Service
 */

@Slf4j
@Service
@Transactional
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class BPInspectPointTypeServiceImpl implements BPInspectPointTypeService {

    @Autowired
    private BPInspectPointTypeRepository inspectPointTypeDao;

    @Autowired
    private BPInspectPointRepository bpInspectPointRepository;


    @Override
    public ResultBean save(BPInspectPointTypeBean bean) {
        List<BPInspectPointType> list = inspectPointTypeDao.findByTypeName(bean.getTypeName());
        BPInspectPointType entity = null;
        //新增保存
        if (bean.getId() == null || bean.getId() == 0) {
            if (list != null && list.size() > 0) {
                return ResultBean.wrapErrorResultDefault("巡检点类型名称已经存在。");
            }
            entity = new BPInspectPointType();
            entity.setTypeCount(0);
        } else {
            list = inspectPointTypeDao.findByTypeNameAndIdIsNot(bean.getTypeName(), bean.getId());
            if (list != null && list.size() > 0) {
                return ResultBean.wrapErrorResultDefault("巡检点类型名称已经存在。");
            }
            //修改保存
            entity = inspectPointTypeDao.findById(bean.getId()).get();
        }
        entity.setTypeName(bean.getTypeName());
        inspectPointTypeDao.save(entity);
        return ResultBean.success(entity);
    }

    @Override
    public ResultBean deleteById(Long id) {
        // 验证是否有检查点引用此类型
        Optional<BPInspectPointType> pointType = inspectPointTypeDao.findById(id);
        if (pointType.isPresent()) {
            if (pointType.get().getTypeCount() > 0) {
                return ResultBean.wrapErrorResultDefault("当前检查点类型已被检查点关联，请先解除关联后再删除。");
            }
        }
        inspectPointTypeDao.deleteById(id);
        return ResultBean.defaultSuccessResult();
    }

    @Override
    public ResultBean list(Pageable pageable) {
        if (pageable.getPageSize() == 20) {
            List<BPInspectPointType> all = inspectPointTypeDao.findAll();
            return ResultBean.success(all);
        } else {
            Page<BPInspectPointType> page = inspectPointTypeDao.findAll(pageable);
            return ResultBean.pageData(page.getContent(), page.getTotalElements());
        }
    }

    @Override
    public BPInspectPointType getById(Long id) {
        return inspectPointTypeDao.findById(id).get();
    }

    @Override
    public List<SimpleSelectListBean> simpleList() {
        List<BPInspectPointType> all = inspectPointTypeDao.findAll();
        if (CollectionUtils.isNotEmpty(all)){
            List<SimpleSelectListBean> simpleList = new ArrayList<>();
            for (BPInspectPointType bpInspectPointType : all) {
                SimpleSelectListBean listBean = new SimpleSelectListBean();
                listBean.setLabel(bpInspectPointType.getTypeName());
                listBean.setValue(bpInspectPointType.getId());
                simpleList.add(listBean);
            }
            return simpleList;
        }
       return null;
    }
}
